package com.cowain.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cowain.base.core.domain.entity.SysRoleMenuEntity;
import com.cowain.base.core.domain.vo.SysRoleVo;
import com.cowain.base.core.domain.entity.SysRoleEntity;
import com.cowain.system.mapper.SysRoleMapper;
import com.cowain.system.service.SysRoleMenuService;
import com.cowain.system.service.SysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author songxz
 * @date 2022/11/9
 */
@Slf4j
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRoleEntity> implements SysRoleService {

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(SysRoleVo role) {
        boolean insertFlag = this.save(role);
        sysRoleMenuService.insertRoleMenu(role);
        return insertFlag;
    }

    /**
     * 修改角色
     *
     * @param role
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean edit(SysRoleVo role) {
        boolean updateFlag = this.updateById(role);
        sysRoleMenuService.remove(new LambdaQueryWrapper<SysRoleMenuEntity>().eq(SysRoleMenuEntity::getRoleId,role.getRoleId()));
        sysRoleMenuService.insertRoleMenu(role);
        return updateFlag;
    }
}
